package pt.mleiria.network.lowlevel;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import pt.mleiria.network.pool.PoolableSocketFactory;
import pt.mleiria.network.pool.SocketPool;
import pt.mleiria.network.utils.ServerConstants;

public class CommandBot {
	private SocketPool pool = null;

	
	public CommandBot() {
		try {
			//final Socket clientSocket = new Socket(ServerConstants.HOST, ServerConstants.PORT);
			PoolableSocketFactory socketPoolFactory = new PoolableSocketFactory();
			pool = new SocketPool(socketPoolFactory);
			final Socket clientSocket = (Socket)pool.borrowObject();
			OutputStream os = null;
			for(int i = 0; i < ServerConstants.commands.length; i++){
				os = clientSocket.getOutputStream();
				//Sending commands to server bot
				os.write(ServerConstants.commands[i]);
				Thread.sleep(2000);
				receiveEchoServer(clientSocket);
			}
			clientSocket.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new CommandBot();
	}
	private void receiveEchoServer(final Socket clientSocket) throws Exception{
		final InputStream is = clientSocket.getInputStream();
		final byte cmdId = (byte)is.read();
		System.out.println("Received from Server: " + cmdId);
	}
}
